<?php

namespace app\admin\controller\shop;

use app\common\controller\Backend;

/**
 * 包厢预定时间管理
 *
 * @icon fa fa-circle-o
 */
class Shoproombook extends Backend
{

    /**
     * Shoproombook模型对象
     * @var \app\admin\model\Shoproombook
     */
    protected $model = null;
    protected $relationSearch = true;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Shoproombook;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(['shop', 'room'])
                ->where($where)
                ->order('end_time', 'desc')
                ->paginate($limit);
            foreach ($list as $k => $v) {


            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    public function detail(){


        $room_id = $this->request->param('room_id');
        $shopRoomModel = new \app\admin\model\Shoproom();
        $room = $shopRoomModel->where('id',$room_id)->find();
        $rooms = array();
        $rooms[] = $room;
        $list = $shopRoomModel->where('shop_id',$room['shop_id'])->select();
        foreach ($list as $key=>$val){
            if($val['id']  != $room_id){
                $rooms[]= $val;
            }
        }

        $time = time();
        $date = array();
        $date[]=$time;
        for($i =1;$i<=4;$i++){
           $time =  strtotime("+".$i." day");
           $date[]=$time;
        }
        $shopRoomBookModel = new \app\admin\model\Shoproombook();

        $data = array();
        foreach ($date as $key=>$val){


            if($key == 0){
                $startTime = time();
            }else{
                $startTime = strtotime(date("Y-m-d",$val)." 00:00:00");
            }
            $endTime = strtotime(date("Y-m-d",$val)." 23:59:59");

            $obj = array();
            $obj['date'] = date("m-d",$val);

            $where = array();
            $where['room_id'] = $room_id;
            $where['end_time'] = array("between",array($startTime,$endTime));

            $list = $shopRoomBookModel->where($where)->select();
            $bookList = array();
            foreach ($list as $k=>$v){
                $obj2 = array();
                $obj2['mobile'] = $v['mobile'];
                if($v['status'] == 'undo'){
                    $obj2['status_txt'] = "未使用";
                }else if($v['status'] == 'doing'){
                    $obj2['status_txt'] = "使用中";
                }
                $obj2['start'] = date("H:i",$v['start_time']);
                $obj2['end'] = date("H:i",$v['end_time']);
                $bookList[] = $obj2;
            }
            $obj['list']  = $bookList;
            $data[] = $obj;
        }

        $this->view->assign("list",$data);
        $this->view->assign("rooms",$rooms);
        $this->view->assign("name",$room['name']);

        return $this->view->fetch();

    }

}
